package com.yuanian.ecsplus.context;

import com.yuanian.businessobject.model.vo.base.BOAreaFieldSetupVO;
import com.yuanian.ecsplus.converter.TypeConverterFactory;
import com.yuanian.ecsplus.exception.EcsPlusException;
import com.yuanian.ecsplus.handler.ResultHandlerFactory;
import com.yuanian.ecsplus.reflect.EcsField;
import com.yuanian.ecsplus.reflect.MetaClass;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * 反射数据的上下文
 */
public class ReflectContext {
    //转化的入口类
    private MetaClass metaClass;

    //要反射的对象的字段
    private EcsField field;

    //map数据
    private Map map;

    //要反射的对象
    private Object object;

    //转换后的结果
    private Object result;

    public ReflectContext(MetaClass metaClass) {
        this.metaClass = metaClass;
    }

    public ReflectContext(MetaClass metaClass, EcsField field, Object object) {
        this.metaClass = metaClass;
        this.field = field;
        this.object = object;
    }

    public ReflectContext(MetaClass metaClass, EcsField field, Object object, Map map) {
        this.metaClass = metaClass;
        this.field = field;
        this.object = object;
        this.map = map;
    }

    public MetaClass getMetaClass() {
        return metaClass;
    }

    public void setMetaClass(MetaClass metaClass) {
        this.metaClass = metaClass;
    }

    public Field getField() {
        return field.getField();
    }

    public BOAreaFieldSetupVO getBOAreaFieldSetupVO() {
        return field.getBoAreaFieldSetupVO();
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public Object getRawResult() throws IllegalAccessException {
        return field.getField().get(object);
    }

    public Object getMapData() {
        return map.get(field.getName());
    }

    public void putMapData(Object data) {
        this.map.put(field.getName(), data);
    }

    public void putMapData() {
        if (result != null) {
            this.map.put(field.getName(), result);
        }
    }

    public void setFieldData() {
        if (result != null) {
            try {
                field.set(object, result);
            } catch (IllegalAccessException e) {
                throw new EcsPlusException(e.getCause());
            }
        }

    }

    public void handleObj2Map() {
        //先计算结算
        this.setResult(TypeConverterFactory.getTypeConverter(field.getType()).obj2Map(this));
        //然后决定结果是否放入map
        ResultHandlerFactory.getHandler(field.getField()).obj2Map(this);
    }

    public void handleMap2Obj() {
        //先计算结果
        this.setResult(TypeConverterFactory.getTypeConverter(field.getType()).map2Obj(this));
        //然后决定结果是否给字段设值
        ResultHandlerFactory.getHandler(field.getField()).map2Obj(this);
    }

}
